using System;
using System.Collections.Generic;
using System.Text;
using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;


namespace updatetoselect
{
    class updatetoselect
    {
        static void Main(string[] args)
        {

            int c = Environment.TickCount;

            TGSqlParser parser = new TGSqlParser(TDbVendor.DbVMssql);
            Console.WriteLine("Time escaped after starting: {0}", Environment.TickCount - c);

            parser.SqlText.Text = "update tablea set fielda = 1,fieldb = 2 where fieldc='myvalue'";

            Console.WriteLine("working..");
            int i = parser.Parse();
            if (i == 0)
            {
                TUpdateSqlStatement stmt = (TUpdateSqlStatement)parser.SqlStatements[0];
                string whereclause = stmt.WhereClauseText;
                string fromclause = stmt.TableText;
                string selecttext = "";

                TLzFieldList flds = stmt.Fields;
                for (int j = 0; j < flds.Count(); j++)
                {
                    string fld = flds[j].FieldExpr.lexpr.AsText;
                    selecttext += fld;
                    if (j != flds.Count() - 1)
                    {
                        selecttext += ",";
                    }
                }
                string selectsql = "select " + selecttext + " from " + fromclause + " where " + whereclause;
                Console.WriteLine(selectsql);

            }
            else
                Console.WriteLine(parser.ErrorMessages);

        }
    }
}
